package com.dreampharos.util;

import java.util.StringTokenizer;

/**
  * Description:
 *
 * @author 
 * @version
 * @since 
 * Revision:
 */
public class StringUtil{
 
   //���ʹ� <br>��ũ�� ��ȯ
	public static String lineBreak(String src) {
    	int len = src.length();
		int linenum = 0, i = 0;

		for (i = 0; i < len; i++)
		  if (src.charAt(i) == '\n')
			linenum++;

		StringBuffer dest = new StringBuffer(len + linenum * 3);

		for (i = 0; i < len; i++) {
		  if (src.charAt(i) == '\n')
			dest.append("<br>");
		  else
			dest.append(src.charAt(i));
		}

		return dest.toString();
	  }

	//html ���ڿ� ó��
	public static String htmlReplace(String str) {
		if(str == null) {
			str = "";
		} else {
			str = str.replaceAll("\"", "&quot;");
			str = str.replaceAll("<", "&lt;");
			str = str.replaceAll(">", "&gt;");
		}
		
		return str;
	}
	
	//DB �Է� ���ڿ� ó��
	public static String setDBReplace(String str) {
		if(str == null) {
			str = "";
		} else {
			str = str.replaceAll("'", "''");
		}
		
		return str.trim();
	}
	
	//DB ��� ���ڿ� ó��
	public static String getDBReplace(String str) {
		if(str == null) {
			str = "";
		} else {
			str = htmlReplace(str);
			str = str.replaceAll("''", "'");
		}
		
		return str;
	}
    
	// ���ڿ��� null�� ��� ���ڿ��� ����
	public static String nullChk(String str) {
		return nullChk(str, "");
	}
   //���ڿ��� null�� ��� ������ ���ڿ��� ����
	public static String nullChk(String str, String newStr) {
		if (str==null) 
			return newStr;
		else 
			return str.trim();
	}
	
	public static String replaceStr(String mainStr, String oldStr, String newStr){
		String mainString= mainStr;
		String oldString=oldStr;
		String newString=newStr;
		StringBuffer mainSb = new StringBuffer(mainString);
		int i = mainString.lastIndexOf(oldString);
			while (i >= 0) {
				mainSb.replace(i, (i + oldString.length()), newString);
				i = mainString.lastIndexOf(oldString, i - 1);
			}
		return mainSb.toString();
	}
	
	public static String replaceStr(String mainStr, String oldStr1, String newStr1, String oldStr2, String newStr2){
		String mainString= mainStr;
		String oldString1=oldStr1;
		String newString1=newStr1;
		String oldString2=oldStr2;
		String newString2=newStr2;
		StringBuffer mainSb = new StringBuffer(mainString);
		int i = mainString.lastIndexOf(oldString1);
			while (i >= 0) {
				mainSb.replace(i, (i + oldString1.length()), newString1);
				i = mainString.lastIndexOf(oldString1, i - 1);
			}
		mainString = mainSb.toString();
		mainSb = new StringBuffer(mainString);
		i = mainString.lastIndexOf(oldString2);
			while (i >= 0) {
				mainSb.replace(i, (i + oldString2.length()), newString2);
				i = mainString.lastIndexOf(oldString2, i - 1);
			}	
		return mainSb.toString();
	}

	public static String replaceStr(String mainStr){
		String mainString= mainStr;
		String oldString1=">";
		String newString1="&gt;";
		String oldString2="<";
		String newString2="&lt;";
		StringBuffer mainSb = new StringBuffer(mainString);
		
		int i = mainString.lastIndexOf(oldString1);		
		while (i >= 0) {
			mainSb.replace(i, (i + oldString1.length()), newString1);
			i = mainString.lastIndexOf(oldString1, i - 1);
		}
		
		mainString = mainSb.toString();
		mainSb = new StringBuffer(mainString);
		
		i = mainString.lastIndexOf(oldString2);		
		while (i >= 0) {
			mainSb.replace(i, (i + oldString2.length()), newString2);
			i = mainString.lastIndexOf(oldString2, i - 1);
		}
		
		return mainSb.toString();
	}
	
	
	public static String removeTag(String s) {
        final int NORMAL_STATE = 0;
        final int TAG_STATE = 1;
        final int START_TAG_STATE = 2;
        final int END_TAG_STATE = 3;
        final int SINGLE_QUOT_STATE = 4;
        final int DOUBLE_QUOT_STATE = 5;
        int state = NORMAL_STATE;
        int oldState = NORMAL_STATE;
        char[] chars = s.toCharArray();
        StringBuffer sb = new StringBuffer();
        char a;
        for (int i = 0; i < chars.length; i++) {
            a = chars[i];
            switch (state) {
                case NORMAL_STATE:
                    if (a == '<')
                        state = TAG_STATE;
                    else
                        sb.append(a);
                    break;
                case TAG_STATE:
                    if (a == '>')
                        state = NORMAL_STATE;
                    else if (a == '\"') {
                        oldState = state;
                        state = DOUBLE_QUOT_STATE;
                    }
                    else if (a == '\'') {
                        oldState = state;
                        state = SINGLE_QUOT_STATE;
                    }
                    else if (a == '/')
                        state = END_TAG_STATE;
                    else if (a != ' ' && a != '\t' && a != '\n' && a != '\r' && a != '\f')
                        state = START_TAG_STATE;
                    break;
                case START_TAG_STATE:
                case END_TAG_STATE:
                    if (a == '>')
                        state = NORMAL_STATE;
                    else if (a == '\"') {
                        oldState = state;
                        state = DOUBLE_QUOT_STATE;
                    }
                    else if (a == '\'') {
                        oldState = state;
                        state = SINGLE_QUOT_STATE;
                    }
                    else if (a == '\"')
                        state = DOUBLE_QUOT_STATE;
                    else if (a == '\'')
                        state = SINGLE_QUOT_STATE;
                    break;
                case DOUBLE_QUOT_STATE:
                    if (a == '\"')
                        state = oldState;
                    break;
                case SINGLE_QUOT_STATE:
                    if (a == '\'')
                        state = oldState;
                    break;
            }
        }
        return sb.toString();
    }

    /** ���� Ȯ���ڸ� ���ϱ�
     *  @param fileName
     *  @return String ���� Ȯ���� �̸�
     */
    public static String getFileExt(String fileName)
    {
        String value = new String();
        int start = 0;
        int end = 0;
        if(fileName == null){
            return null;
        } else {
            start = fileName.lastIndexOf('.') + 1;
            end = fileName.length();
            value = fileName.substring(start, end);
            return value;
        }
    }

    /**
     * String�� ���ʿ� �־��� char�� ä�� �־��� ����(len)�� ��Ʈ���� ��ȯ�Ѵ�.
     *
     * <p>
     * @param		str ��� ���ڿ�.
     * @param		fc ä�� ����.
     * @param		len ��ȯ�� ���ڿ��� ����.
     * @return		��ȯ�� ���ڿ�.
     */
	public static String lPad(String str, char fc, int len) {
		if (str == null) {
			str = "";
}		if (String.valueOf(fc).length() <= 0) {
			throw new IllegalArgumentException("fc must provied");
		}
		if (len <= 0) {
			throw new IllegalArgumentException("length must be positive");
		}

		for (; str.length() < len; str = fc + str);

		return str;
	}


    /**
     * String�� ���ʿ� �־��� char�� ä�� �־��� ����(len)�� ��Ʈ���� ��ȯ�Ѵ�.
     *
     * <p>
     * @param		str ��� ���ڿ�.
     * @param		fc ä�� ����.
     * @param		len ��ȯ�� ���ڿ��� ����.
     * @return		��ȯ�� ���ڿ�.
     */
	public static String rPad(String str, char fc, int len) {
		if (str == null) {
			str = "";
		}
		if (String.valueOf(fc).length() <= 0) {
			throw new IllegalArgumentException("fc must provied");
		}
		if (len <= 0) {
			throw new IllegalArgumentException("length must be positive");
		}

		for (; str.length() < len; str = str + fc);

		return str;
	}


    /**
     * ���ڿ����� ���ڸ� ���� ���ο� ���ڿ��� ��ȯ�Ѵ�.
     *
     * <p>
     * @param		str ��� ���ڿ�.
     * @return		��ȯ�� ���ڿ�.
     */
	public static String getOnlyDigit(String str) {
		if (str == null) {
			return null;
		}

		char[] charArr = str.toCharArray();
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < charArr.length; i++) {
			if (Character.isDigit(charArr[i])) {
				sb.append(charArr[i]);
			}
		}

		return sb.toString();
	}


    /**
     * ���ڿ����� ���ڸ� ���� ���ο� ���ڿ��� ��ȯ�Ѵ�.
     *
     * <p>
     * @param		str ��� ���ڿ�.
     * @return		��ȯ�� ���ڿ�.
     */
	public static String getOnlyLetter(String str) {
		if (str == null) {
			return null;
		}

		char[] charArr = str.toCharArray();
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < charArr.length; i++) {
			if (Character.isLetter(charArr[i])) {
				sb.append(charArr[i]);
			}
		}

		return sb.toString();
	}


    /**
     * ���ڿ����� ���ڿ� ���ڸ� ���� ���ο� ���ڿ��� ��ȯ�Ѵ�.
     *
     * <p>
     * @param		str ��� ���ڿ�.
     * @return		��ȯ�� ���ڿ�.
     */
	public static String getOnlyLetterOrDigit(String str) {
		if (str == null) {
			return null;
		}

		char[] charArr = str.toCharArray();
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < charArr.length; i++) {
			if (Character.isLetterOrDigit(charArr[i])) {
				sb.append(charArr[i]);
			}
		}

		return sb.toString();
	}


    /**
     * map�� �����Ͽ� �־��� ���ڿ��� ������ ġȯ�Ѵ�.
	 * <p>
	 * ��뿹 :
	 * <p><blockquote><pre>
	 * String a = "This is a String";
	 * String[][] map = {{"s","t"},{"i","k"},{"S","o"}};
	 * out.print(StringUtil.replace(a, map));
	 * ===> Thkt kt a otrkng
	 * </pre></blockquote>
	 * <p>
     * @param		str ��� ���ڿ�.
     * @param		map ��ȯ�� ������ ���� 2���� ���ڿ��迭.
     * @return		��ȯ�� ���ڿ�.
     */
	public static String replace(String str, String map[][]) {
		return replace(str, map, true);
	}


    /**
     * �־��� ���ڿ��� ������ ġȯ�Ѵ�.
	 * <p>
     * @param		str ��� ���ڿ�.
     * @param		find ã�� ���ڿ�.
     * @param		to ġȯ�� ���ڿ�.
     * @return		��ȯ�� ���ڿ�.
     */
	public static String replace(String str, String find, String to) {
		return replace(str, new String[][] {new String[] {find, to}});
	}


    /**
     * �־��� ���ڿ��� ������ ġȯ�Ѵ�. Case�� Insenstive �ϴ�.
	 * <p>
     * @param		str ��� ���ڿ�.
     * @param		find ã�� ���ڿ�.
     * @param		to ġȯ�� ���ڿ�.
     * @return		��ȯ�� ���ڿ�.
     */
	public static String replaceIgnoreCase(String str, String find, String to) {
		return replace(str, new String[][] {new String[] {find, to}}, false);
	}


    /**
     * map�� �����Ͽ� �־��� ���ڿ��� ������ ġȯ�Ѵ�. Case�� Insenstive �ϴ�.
	 * <p>
     * @param		str ��� ���ڿ�.
     * @param		map ��ȯ�� ������ ���� 2���� ���ڿ��迭.
     * @param		caseSensitive Case�� Sensitive���� ����. true �� Case Senstive�̴�.
     * @return		��ȯ�� ���ڿ�.
     */
	public static String replace(String str, String[][] map, boolean caseSensitive) {
		if (str == null) {
			return null;
		}
		if (map.length <= 0) {
			throw new IllegalArgumentException("mapping array cannot be null");
		}

		String original = str;
		if(!caseSensitive) {
			str = str.toUpperCase();
		}

		StringBuffer sb = new StringBuffer();
		int nextCmpPoint = 0;

		do {
			int matchIndex = -1;
			int fastestMatchPoint = str.length();
			String from;
			for(int i = 0; i < map.length; i++) {
				from = map[i][0];
				if(!caseSensitive) {
					from = from.toUpperCase();
				}
				int matchPoint = str.indexOf(from, nextCmpPoint);
				if(matchPoint > -1 && matchPoint <= fastestMatchPoint) {
					fastestMatchPoint = matchPoint;
					matchIndex = i;
				}
			}

			sb.append(original.substring(nextCmpPoint, fastestMatchPoint));
			if(matchIndex < 0) {
				break;
			}
			from = map[matchIndex][0];
			String to = map[matchIndex][1];
			sb.append(to);
			nextCmpPoint = fastestMatchPoint + from.length();
		} while(nextCmpPoint < str.length());

		return sb.toString();
	}


    /**
     * ���ڿ��� delimiter�� �и��ؼ� ���ڿ� �迭 ���·� ��ȯ�Ѵ�.
	 * <p>
     * @param		str ��� ���ڿ�.
     * @param		delimiter ������.
     * @return		������ ���ڿ��� �迭.
     */
	public static String[] split(String str, String delimiter) {
		if (str == null) {
			return null;
		}
		if (delimiter == null) {
			throw new NullPointerException("delimiter cannot be null");
		}

		StringTokenizer st = new StringTokenizer(str, delimiter);
		String[] strarr = new String[st.countTokens()];

		for (int i = 0; i < strarr.length; i++) {
			strarr[i] = st.nextToken();
		 }

		return strarr;
	}


    /**
     * ���ڿ� �迭�� delimiter�� �����Ͽ� �ϳ��� ���ڿ��� ��ȯ��.
	 * <p>
     * @param		strarr ��� ���ڿ� �迭.
     * @param		delimiter ������.
     * @return		������ ���ڿ�.
     */
	public static String join(String[] strarr, String delimiter) {
		if (strarr == null) {
			return null;
		}
		if (delimiter == null) {
			throw new NullPointerException("delimiter cannot be null");
		}

		String str = "";

		for (int i = 0; i < strarr.length; i++) {
			str += strarr[i];
			if (i < (strarr.length - 1)) {
				str += delimiter;
			}
		 }

		return str;
	}


    /**
     * ���ڿ��� ���� �ִ��� �˻��Ѵ�.
	 * <p>
     * @param		str ��� ���ڿ�.
     * @return		���ڿ��� null �̰ų� ���̰� 0�� ��� false��, ������ ���� true�� �����Ѵ�.
     */
	public static boolean hasValue(String str) {
		if (str != null && str.length() > 0) {
			return true;
		} else {
			return false;
		}
	}


    /**
     * ���ڿ��� null�̸� �־��� �⺻������ ġȯ�Ѵ�.
	 * <p>
     * @param		str ��� ���ڿ�.
     * @param		defaultValue null�� ��� ġȯ�� ���ڿ�.
     * @return		���ڿ��� null �̰ų� ���̰� 0�� ��� false��, ������ ���� true�� �����Ѵ�..
     */
	public static String fixNull(String str, String defaultValue) {
		return str == null ? defaultValue : str;
	}


    /**
     * ���ڿ��� null�̸� ""(���ڿ�)�� ġȯ�Ѵ�.
	 * <p>
     * @param		str ��� ���ڿ�.
     * @return		���ڿ��� null �̰ų� ���̰� 0�� ��� false��, ������ ���� true�� �����Ѵ�.
     */
	public static String fixNull(String str) {
		return fixNull(str, "");
	}


    /**
     * ���ڿ��� ���̸� ��ȯ�Ѵ�. �ѱ��� ��� 2 byte�� ����Ѵ�.
	 * <p>
     * @param		str ��� ���ڿ�.
     * @return		���ڿ��� ����.
     */
	public static int getLength(String str) {
		if (str == null) {
			return 0;
		}

		int strlen = 0;
		char c;

		for(int i = 0;  i < str.length(); i++) {
			c = str.charAt(i);

			if ( c  <  0xac00 || 0xd7a3 < c ) {
				strlen++;
			} else {
				strlen += 2;
			}
		}

	   return strlen;
	}

	// ȭ�鿡�� �迭�� ����Ÿ�� �Ѿ���� ��� �����ڷ� ������ �и�
	 public static String[] getStringArray(StringTokenizer st)
	    {
	        int size=st.countTokens();
	        String rs[] = new String[size+1];
	        int i=0;
	        while(st.hasMoreTokens()){
	             rs[i] = st.nextToken();
	             i++;
	        }
	        return rs;
	    }

		/**
		* Class �׽�Ʈ
		* @
		* @
		* @return	String ���ڿ�...
		*/
		public static String cutString(String str, int idx)
		{
			/*if(str.length() > idx){
				str = str.substring(0,idx) + "....";
			}
			return str;*/

			StringBuffer	sb			=	new	StringBuffer();
			int			nSourceLen	=	str.getBytes().length;
			byte[]			bSource		=	str.getBytes();
			byte[]			bUniChar	=	new	byte[2];

			if( str == null )
				return "";

			if(nSourceLen < idx)
			idx	=	nSourceLen;

			for(int i = 0; i < idx; i++) {
				/*	unicode(over ascii:128) �� ��� */
				if(bSource[i] < 0) {
					if(i == idx-1)	break;
					bUniChar[0] = bSource[i];
					bUniChar[1] = bSource[i+1];

					sb.append(new String(bUniChar));
					i++;
				} else {
					sb.append((char)bSource[i]);
				}
			}

			if ( !str.equals( sb.toString() ) ) {
				sb.append("...");
			}
			return	sb.toString();


		}//end showString method

		/**
		* Class �׽�Ʈ
		* @
		* @return	String "00"+���ڿ�...
		*/
		public static String setZeroString(String str)
		{

			StringBuffer	sb			=	new	StringBuffer();
			int			nSourceLen	=	str.length();

			if( str == null )
				return "";

			if(nSourceLen == 0){
				sb.append("00");
			}else if (nSourceLen ==1){
				sb.append("0");
				sb.append(str);
			}else {
				sb.append(str);
			}

			return	sb.toString();
		}//end setZeroString method
}